package cn.ffcs.search.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.ffcs.base.dao.BaseDAO;
import cn.ffcs.base.web.PageControl;
import cn.ffcs.common.model.Attach;
import cn.ffcs.product.model.Product;
import cn.ffcs.product.model.ProductManager;
import cn.ffcs.search.model.Condition;
import cn.ffcs.search.model.Search;
/**
 * 产品目录实体类：定义基本属性<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">g.huangjx</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:2009-2-26</td>
 * <td>修改人:g.huanjx</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author g.huangjx
 * @version $Id$
 */
public class SearchDAO extends HibernateDaoSupport implements ISearchDAO
{
    /*
     * 统一检索
     */
    public List<Search> findAllSearch(Condition conditon,int[] str){
       StringBuffer hsqb = new StringBuffer("from Search a where 1=1 ");
       if(!conditon.getTitle().equals("")){
           if(conditon.getMatching().equals("like")){
               hsqb.append(" and a.title like '%"+conditon.getTitle()+"%'");
           }else{
               hsqb.append(" and a.title = '"+conditon.getTitle()+"'");
           }
       }
       if(!conditon.getCreator().equals("")){
           if(conditon.getMatching().equals("like")){
               hsqb.append(" and a.creator like '%"+conditon.getCreator()+"%'");
           }else{
               hsqb.append(" and a.creator = '"+conditon.getCreator()+"'");
           }
       }
       if(!conditon.getBegin().equals("")){
           hsqb.append(" and to_char(a.begin,'yyyy-mm-dd')>='"+conditon.getBegin()+"'");
       }
       if(!conditon.getEnd().equals("")){
           hsqb.append(" and to_char(a.begin,'yyyy-mm-dd')<='"+conditon.getEnd()+"'");
       }
       if(conditon.getModules().length<2){
           hsqb.append(" and a.module = '"+conditon.getModules()[0]+"'");
       }
       hsqb.append(" order by a.begin "+conditon.getAscdesc());
       return getList(hsqb.toString(),str);
    }
    /*
     * 获取当前面页的数据公用共部分调用
     */
    public List getList(String hsq,int[] str){
        String countHsql=hsq;
        if(hsq.toLowerCase().indexOf(" order ")!=-1 && hsq.toLowerCase().indexOf(" by ")!=-1 ){
            countHsql=hsq.substring(0,hsq.lastIndexOf("order"));
        }
        List count=super.getSession().createQuery("select count(*) "+countHsql).list();
        str[0]=Integer.parseInt(count.get(0).toString());
        int firstRow=str[1]*(str[2]-1);
        int maxRow=str[1];
        return super.getSession().createQuery(hsq).setFirstResult(firstRow).setMaxResults(maxRow).list();
    }
    
    /**
     * 根据用户名模糊查询产品经理人
     * TODO 请输入方法说明
     * @param conds
     * @return
     */
    public List<ProductManager> findProductManagerView(String name){
        String hql = "select prod from ProductManager prod,Person person where prod.person=person.id and prod.end>sysdate ";
        if (name != null)
        {
            hql+=" and person.name like '%"+ name+"%'";
        }
        hql+=" order by prod.person,prod.isPrimary desc";
        return this.getSession().createQuery(hql).list();
    }
    
    /*
     * 统一检索附件查询
     */
    public List<Search> findAllSearchByAnnex(Condition conditon, int[] str)
    {
        String content = encode(conditon.getContent());
        StringBuffer hsqb = new StringBuffer(" from Attach a where dbms_lob.instr(a.content, '"+content+"', 1, 1) > 1 and (a.refEntity='cn.ffcs.product.model.ProductReference' or a.refEntity='cn.ffcs.share.model.ShareReference') ");

       if(!conditon.getCreator().equals("")){
           if(conditon.getMatching().equals("like")){
               hsqb.append(" and a.creator like '%"+conditon.getCreator()+"%'");
           }else{
               hsqb.append(" and a.creator = '"+conditon.getCreator()+"'");
           }
       }
       if(!conditon.getBegin().equals("")){
           hsqb.append(" and to_char(a.begin,'yyyy-mm-dd')>='"+conditon.getBegin()+"'");
       }
       if(!conditon.getEnd().equals("")){
           hsqb.append(" and to_char(a.begin,'yyyy-mm-dd')<='"+conditon.getEnd()+"'");
       }
       if(conditon.getModules().length<2){
           hsqb.append(" and a.module = '"+conditon.getModules()[0]+"'");
       }
       hsqb.append(" and a.end>sysdate ");
       hsqb.append(" order by a.begin "+conditon.getAscdesc());

       List list=getList(hsqb.toString(),str);
       List<Search> searchs=new ArrayList();
       Search search=null;
       Attach a;
       for(int i=0;i<list.size();i++){
           search=new Search();
           a=(Attach)list.get(i);
           search.setModuleid(new Integer(a.getRefEntityID()));
           search.setTitle(a.getFullName());
           search.setCreator(a.getCreator());
           search.setBegin(a.getBegin());
           search.setEnd(a.getEnd());
           if("cn.ffcs.share.model.ShareReference".equals(a.getRefEntity())){
               search.setModule("1");
           }
           else if("cn.ffcs.product.model.ProductReference".equals(a.getRefEntity())){
               search.setModule("2");
           }
           searchs.add(search);
       }
       return searchs;
    }
    
    private static String hexString = "0123456789ABCDEF"; //此处不可随意改动
    
    /*
    * 将字符串编码成16进制数字 
    */
    public static String encode(String str) {
       // 根据默认编码获取字节数组
       byte[] bytes = str.getBytes();
       StringBuilder sb = new StringBuilder(bytes.length * 2);
       // 将字节数组中每个字节拆解成2位16进制整数
       for (int i = 0; i < bytes.length; i++) {
        sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
        sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
       }
       return sb.toString();
    }

}
